jQuery(document).ready(function($){
$("#header").load("/job/topul.html");
    showAll();



    //查询当前登录用户
    function showAll() {
    $.ajax({
        url: "user/info",
        type: "get",
        dataType: "json",
        success: function (data) {
            console.log(data);
            let nowuserdata=data.data.data;
            $("#name").val(nowuserdata.name);
            $("#mobile").val(nowuserdata.mobile);
            $("#age").val(nowuserdata.age);
            $("#sex").val(nowuserdata.sex);
            $("#email").val(nowuserdata.email);

            $("#name2").val(nowuserdata.name);
            $("#mobile2").val(nowuserdata.mobile);
            $("#age2").val(nowuserdata.age);
            $("#email2").val(nowuserdata.email);
        }
    })
    }
    //查询当前登录用户结束

    //修改个人信息开始
    $("button[name='updateuser']").click(function(){
    if(confirm("确认修改吗？")){
    let updateuserData={
                  name:$("#name2").val(),
                  mobile :$("#mobile2").val(),
                  age:$("#age2").val(),
                  sex:$("#sex2").val(),
                  email :$("#email2").val()};
    $.ajax({
    url : "/job/user/infoEdit",
                async : false,
                type : "POST",
                contentType : 'application/json',
                dataType : 'json',
                data :JSON.stringify(updateuserData),
                success : function(data) {
                if(data.msg=="成功"){
                alert("修改成功！");
                window.location.href="/job/myProfile.html";
                }else alert("修改失败！")
                }
    });
    }else{console.log("取消修改")};
    });
    //修改个人信息开始

    //密码修改
    $("button[name='updatpwd']").click(function(){
            let user =  {"pwd":$("#pwd0").val()}
               $.ajax({
                   url : "user/checkPwd",
                   data : JSON.stringify(user),
                   type : "post",
                   contentType: 'application/json;charset=utf-8',
                   dataType : "json",
                   success : function(data){
                   if(data.data =="成功"){
                       pwd();
                       }else{
                       alert("原密码不正确！请重新输入");
//                       $(location).attr("href", "myProfile.html");
                       }
                   }
               })
    })

    function pwd(){
            let p1=$("#pwd1").val();
             let p2=$("#pwd2").val();
             if(p1!=p2){
                 alert("两次密码输入不一致！请重新输入");
             }else{
         var user = {"pwd":$("#pwd1").val()}
         if(confirm("确认修改吗?")){
             //请求
             $.ajax({
                 url : "user/pwdEdit",
                 data : JSON.stringify(user),
                 type : "post",
                 contentType: 'application/json;charset=utf-8',
                 dataType : "json",
                 success : function(data){
                     alert("密码修改成功")
                     $(location).attr("href", "login.html");
                 }
             })
             //请求end
         }else{
             console.log("取消修改")
         }
         }
    }
    //密码修改结束
});